/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package expression.interval;

import java.util.Comparator;

/**
 *
 * @author ddoose
 */
public class IntComparator implements Comparator {

    public int compare(Object o1, Object o2) {
        // EMPTY
        if (o1 instanceof IntEmpty) {
            if (o2 instanceof IntEmpty) {
                return 0;
            }
            else {
                return -1;
            }
        }
        if (o2 instanceof IntEmpty) {
            return 1;
        }
        // UNIVERSE
        if (o1 instanceof IntUniverse) {
            if (o2 instanceof IntUniverse) {
                return 0;
            }
            else {
                return -1;
            }
        }
        if (o2 instanceof IntUniverse) {
            return 1;
        }
        // RIGHT BOUNDED
        if (o1 instanceof IntRightBounded) {
            if (o2 instanceof IntRightBounded) {
                IntRightBounded i = (IntRightBounded)o1;
                IntRightBounded j = (IntRightBounded)o2;
                if (i.getRightBound() == j.getRightBound()) {
                    return 0;
                }
                else if (i.getRightBound() > j.getRightBound()) {
                    return 1;
                }
            }
            return -1;
        }
        if (o2 instanceof IntRightBounded) {
            return 1;
        }
        // OTHERS
        int i;
        if (o1 instanceof IntValue) {
            i = ((IntValue)o1).getLeftBound();
        }
        else if (o1 instanceof IntLeftBounded) {
            i = ((IntLeftBounded)o1).getLeftBound();
        }
        else if (o1 instanceof IntBounded) {
            i = ((IntBounded)o1).getLeftBound();
        }
        else {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        int j;
        if (o2 instanceof IntValue) {
            j = ((IntValue)o2).getLeftBound();
        }
        else if (o2 instanceof IntLeftBounded) {
            j = ((IntLeftBounded)o2).getLeftBound();
        }
        else if (o2 instanceof IntBounded) {
            j = ((IntBounded)o2).getLeftBound();
        }
        else {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        if (i == j) {
            return 0;
        }
        else if (i < j) {
            return -1;
        }
        else {
            return 1;
        }
    }
}
